package com.honey.mybatis.generator;

import org.java.plugin.Plugin;

import com.honey.compilation.java.AbstractJavaCompilation;
import com.honey.compilation.java.Field;
import com.honey.compilation.java.InnerClass;
import com.honey.compilation.java.InnerEnum;
import com.honey.compilation.java.Method;
import com.honey.compilation.java.StaticBlock;
import com.honey.core.compiler.CompilationException;
import com.honey.core.compiler.ContentType;
import com.honey.core.dbmapping.introspect.IntrospectSchema;
import com.honey.core.dbmapping.introspect.IntrospectSchemaColumn;
import com.honey.core.types.FullyQualifiedJavaType;
import com.honey.core.types.JDKFullyQualifiedJavaType;
import com.honey.generator.java.extension.point.GeneratorJavaClass;
import com.honey.mybatis.generator.MybatisFactory.MybatisClass;

/**
 * 生成GeneratedCriteria 类
 * @author Administrator
 *
 */
public class MybatisGeneratedCriteriaExtension extends AbstractMybatisExtension  implements  GeneratorJavaClass{
	
	AbstractProduceJavaCompilation produceJavaCompilation = null;
	
	private AbstractJavaCompilation abstractJavaCompilation = null;
	
	public MybatisGeneratedCriteriaExtension(Plugin belongPlugin) {
		super(belongPlugin);
	}
	
	@Override
	public AbstractJavaCompilation createNewCompilation( IntrospectSchema introspectSchema, IntrospectSchemaColumn introspectSchemaColumn) {
		produceJavaCompilation = MybatisFactory.getAbstractProduceJavaCompilation(
				MybatisClass.GENERATED_CRITERIA, 
				mybatisPlugin, 
				introspectSchema, 
				introspectSchemaColumn
		);

		try {
			abstractJavaCompilation = produceJavaCompilation.produceCompilation();
		} catch (CompilationException e) {
			e.printStackTrace();
		}
		return abstractJavaCompilation;
	}

	@Override
	public FullyQualifiedJavaType getType() {
		return abstractJavaCompilation.getType();
	}
	
	@Override
	public Field[] importFields() {
//		Field [] answer = new Field[14];
//		Field field = new Field(CriteriaConstant.CRITERION_FIELD_NAME ,new FullyQualifiedJavaType("java.util.List<"+CriteriaConstant.CRITERION_CLASS_NAME+"> "),new Decoration(JavaVisibility.PROTECTED));
//		answer[0] = field;
//		field = new Field("IS_NULL" ,JDKFullyQualifiedJavaType.getStringInstance(),new Decoration(JavaVisibility.PRIVATE,true,true));
//		field.setInitializationString(" is null");
//		field.addJavaDocLine("is null 条件。");
//		answer[1] = field;
//		
//		field = new Field("IS_NOT_NULL" ,JDKFullyQualifiedJavaType.getStringInstance(),new Decoration(JavaVisibility.PRIVATE,true,true));
//		field.setInitializationString(" is not null");
//		field.addJavaDocLine(" is not null条件。");
//		answer[2] = field;
//		
//		field = new Field("EQUAL_TO" ,JDKFullyQualifiedJavaType.getStringInstance(),new Decoration(JavaVisibility.PRIVATE,true,true));
//		field.setInitializationString(" = ");
//		field.addJavaDocLine("= 条件。");
//		answer[3] = field;
//		
//		field = new Field("NOT_EQUAL_TO" ,JDKFullyQualifiedJavaType.getStringInstance(),new Decoration(JavaVisibility.PRIVATE,true,true));
//		field.setInitializationString(" <> ");
//		field.addJavaDocLine("<> 条件。");
//		answer[4] = field;
//		
//		field = new Field("GREATER_THAN" ,JDKFullyQualifiedJavaType.getStringInstance(),new Decoration(JavaVisibility.PRIVATE,true,true));
//		field.setInitializationString(" > ");
//		field.addJavaDocLine("> 条件。");
//		answer[5] = field;
//		
//		field = new Field("GREATER_THAN_OR_EQUAL_TO" ,JDKFullyQualifiedJavaType.getStringInstance(),new Decoration(JavaVisibility.PRIVATE,true,true));
//		field.setInitializationString(" >= ");
//		field.addJavaDocLine(">= 条件。");
//		answer[6] = field;
//		
//		field = new Field("LESS_THAN" ,JDKFullyQualifiedJavaType.getStringInstance(),new Decoration(JavaVisibility.PRIVATE,true,true));
//		field.setInitializationString(" < ");
//		field.addJavaDocLine("< 条件。");
//		answer[7] = field;
//		
//		field = new Field("LESS_THAN_OR_EQUAL_TO" ,JDKFullyQualifiedJavaType.getStringInstance(),new Decoration(JavaVisibility.PRIVATE,true,true));
//		field.setInitializationString(" <= ");
//		field.addJavaDocLine("<= 条件。");
//		answer[8] = field;
//		
//		field = new Field("IN" ,JDKFullyQualifiedJavaType.getStringInstance(),new Decoration(JavaVisibility.PRIVATE,true,true));
//		field.setInitializationString(" in ");
//		field.addJavaDocLine("in 条件。");
//		answer[9] = field;
//		
//		field = new Field("NOT_IN" ,JDKFullyQualifiedJavaType.getStringInstance(),new Decoration(JavaVisibility.PRIVATE,true,true));
//		field.setInitializationString(" not in ");
//		field.addJavaDocLine("not in 条件。");
//		answer[10] = field;
//		
//		field = new Field("BETWEEN" ,JDKFullyQualifiedJavaType.getStringInstance(),new Decoration(JavaVisibility.PRIVATE,true,true));
//		field.setInitializationString(" between ");
//		field.addJavaDocLine("between 条件。");
//		answer[11] = field;
//		
//		field = new Field("NOT_BETWEEN" ,JDKFullyQualifiedJavaType.getStringInstance(),new Decoration(JavaVisibility.PRIVATE,true,true));
//		field.setInitializationString(" not between ");
//		field.addJavaDocLine("not between 条件。");
//		answer[12] = field;
//		
//		field = new Field("LIKE" ,JDKFullyQualifiedJavaType.getStringInstance(),new Decoration(JavaVisibility.PRIVATE,true,true));
//		field.setInitializationString(" like ");
//		field.addJavaDocLine("like 条件。");
//		answer[13] = field;
//		
		
//		return answer;
		Field[] answer = null;
		try {
			answer = produceJavaCompilation.produceFields();
		} catch (CompilationException e) {
			e.printStackTrace();
		}
		return answer;
	}

	@Override
	public Method[] importMethods() {
//		return new Method[]{
//					
//			(new CreateIsValidMethod(mybatisPlugin)).createMethod(null, null, null, null),
//			
//			(new CreateGetAllCriteriaMethod(mybatisPlugin)).createMethod(null, null, null, null),
//			
//			(new CreateGetCriteriaMethod(mybatisPlugin)).createMethod(null, null, null, null),
//			
//			(new CreateNoValueConditionMethod(mybatisPlugin,"andIsNull","IS_NULL","is null")).createMethod(null, null, null, null),
//			
//			(new CreateNoValueConditionMethod(mybatisPlugin,"andIsNotNull","IS_NOT_NULL","is not null")).createMethod(null, null, null, null),
//			
//			(new CreateSingleValueConditionMethod(mybatisPlugin,"andEqualTo", "EQUAL_TO", "=")).createMethod(null, null, null, null),
//			
//			(new CreateSingleValueConditionMethod(mybatisPlugin,"andNotEqualTo", "NOT_EQUAL_TO", "<>")).createMethod(null, null, null, null),
//			
//			(new CreateSingleValueConditionMethod(mybatisPlugin,"andGreaterThan", "GREATER_THAN", ">")).createMethod(null, null, null, null),
//			
//			(new CreateSingleValueConditionMethod(mybatisPlugin,"andGreaterThanOrEqualTo", "GREATER_THAN_OR_EQUAL_TO", ">=")).createMethod(null, null, null, null),
//			
//			(new CreateSingleValueConditionMethod(mybatisPlugin,"andLessThan", "LESS_THAN", "<")).createMethod(null, null, null, null),
//			
//			(new CreateSingleValueConditionMethod(mybatisPlugin,"andLessThanOrEqualTo", "LESS_THAN_OR_EQUAL_TO", "<=")).createMethod(null, null, null, null),
//			
//			(new CreateInConditionMethod(mybatisPlugin,"andIn", "IN", "in")).createMethod(null, null, null, null),
//			
//			(new CreateInArrayConditionMethod(mybatisPlugin,"andIn", "IN", "in")).createMethod(null, null, null, null),
//			
//			(new CreateInConditionMethod(mybatisPlugin,"andNotIn", "NOT_IN", "not in")).createMethod(null, null, null, null),
//			
//			(new CreateInArrayConditionMethod(mybatisPlugin,"andNotIn", "NOT_IN", "not in")).createMethod(null, null, null, null),
//			
//			(new CreateBetweenConditionMethod(mybatisPlugin,"andBetween", "BETWEEN", "between")).createMethod(null, null, null, null),
//			
//			(new CreateBetweenConditionMethod(mybatisPlugin,"andNotBetween", "NOT_BETWEEN", "not between")).createMethod(null, null, null, null),
//			
//			(new CreateLikeConditionMethod(mybatisPlugin,"andLike", "LIKE", "like")).createMethod(null, null, null, null),
//			
//			(new CreateAddCriterionNoValueMethod(mybatisPlugin)).createMethod(null, null, null, null),
//			
//			(new CreateAddCriterionSingleValueMethod(mybatisPlugin)).createMethod(null, null, null, null),
//			
//			(new CreateAddCriterionDoubleValueMethod(mybatisPlugin)).createMethod(null, null, null, null),
//		};
		Method[] answer = null;
		try {
			answer = produceJavaCompilation.produceMethods();
		} catch (CompilationException e) {
			e.printStackTrace();
		}
		return answer;
	}
	
	@Override
	public FullyQualifiedJavaType[] importImportClass() {
		
		return new FullyQualifiedJavaType[]{
			JDKFullyQualifiedJavaType.getArrayListInstance(),
		};
	}
	
	@Override
	public InnerClass[] importInnerClass() {
		InnerClass[]  answer = null;
		try {
			AbstractProduceJavaCompilation c = MybatisFactory.getAbstractProduceJavaCompilation(MybatisFactory.MybatisClass.CRITERION, mybatisPlugin, null, null);
			AbstractJavaCompilation inner =  c.produceCompilation();
			Field []fields = c.produceFields();
			inner.addField(fields);
			Method []methods = c.produceMethods();
			inner.addMethod(methods);
			answer=new InnerClass[]{
				(InnerClass)inner	
			};
			
		} catch (CompilationException e) {
			e.printStackTrace();
		}		
		return answer;
	}

	@Override
	public InnerEnum[] importInnerEnumClass() {
		InnerEnum[]  answer = null;
		try {
			answer = produceJavaCompilation.produceInnerEnumClass();
		} catch (CompilationException e) {
			e.printStackTrace();
		}
		return answer;
	}
	
	@Override
	public StaticBlock[] importStaticBlocks() {
		return null;
	}
	
	@Override
	public void clean() {
		
	}
	
	/* (non-Javadoc)
	 * @see com.honey.generator.java.extension.point.GeneratorJavaClass#getContentType()
	 */
	@Override
	public ContentType getContentType() {
		
		return ContentType.JAVA_MODEL;
	}
}
